#import "AppDelegate.h"
#import "AppViewController.h"
// Scenes
#import "GameScene.h"


@interface AppDelegate () {
}

@end


@implementation AppDelegate


#pragma mark - Cleanup memory
- (void) dealloc {
    safeRelease(_appViewController);
	safeRelease(_window);
	[super dealloc];
}


#pragma mark - Application's lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	if (![[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] || ![[UIDevice currentDevice] isMultitaskingSupported]) return NO;
    [application setStatusBarHidden:YES];
    
    // Initialize Window
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // Initialize CCDirector
    [kDirector setProjection:kCCDirectorProjection2D];
    [kDirector setAnimationInterval:1.0f/60.0f];
	[kDirector setDisplayStats:YES];
    
    // Create a Navigation Controller with the Director
    _appViewController.viewControllers = [NSArray arrayWithObject:kDirector];
    
	// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
	CCGLView *glView = [CCGLView viewWithFrame:[_window bounds]
								   pixelFormat:kEAGLColorFormatRGB565
								   depthFormat:0
							preserveBackbuffer:NO
									sharegroup:nil
								 multiSampling:NO
							   numberOfSamples:0];
	[glView setMultipleTouchEnabled:YES];
    
    [kDirector setView:glView];
    [kDirector setWantsFullScreenLayout:YES];
    if(![kDirector enableRetinaDisplay:YES]) DLog(@"Retina Display Not supported!");

	// Navigation Controller
	_appViewController = [[AppViewController alloc] initWithRootViewController:kDirector];
    [_appViewController setNavigationBarHidden:YES];
    [kDirector setDelegate:_appViewController];
    
    // Define texture2D
	[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
    [CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

	CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
	[sharedFileUtils setEnableFallbackSuffixes:NO];
	[sharedFileUtils setiPadSuffix:@"-ipad"];
    [sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];
	[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"];

	// Initialize window
    [_window setRootViewController:_appViewController];
    [_window makeKeyAndVisible];
    
    // Display the first scene
	[kDirector pushScene:[GameScene scene]];
	return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    if([_appViewController visibleViewController] == kDirector)
        [kDirector pause];
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
    if([_appViewController visibleViewController] == kDirector)
        [kDirector stopAnimation];
}
- (void)applicationWillEnterForeground:(UIApplication*)application {
    if([_appViewController visibleViewController] == kDirector)
        [kDirector startAnimation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    if([_appViewController visibleViewController] == kDirector)
        [kDirector resume];
}
- (void)applicationWillTerminate:(UIApplication *)application {
    CC_DIRECTOR_END();
}


#pragma mark - Application's memory handler
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	[kDirector purgeCachedData];
}


#pragma mark - Application's time handler
- (void)applicationSignificantTimeChange:(UIApplication *)application {
	[kDirector setNextDeltaTimeZero:YES];
}

@end

